1 module unde.games.dizzy.omega.item;
2 
3 import derelict.opengl3.gl;
4 import std.conv;
5 import std.math;
6 import unde.games.object;
7 import unde.games.renderer;
8 import unde.global_state;
9 
10 class Item:StaticGameObject
11 {
12     bool inventory;
13     bool used;
14     string model;
15 
16     float def_x, def_y, def_z;
17 
18     this(MainGameObject root, float[3] coords, string model)
19     {
20         def_x = x = coords[0];
21         def_y = y = coords[1];
22         def_z = z = coords[2];
23         
24         models[model] = root.models[model];
25         this.model = model;
26 
27         super(root);
28     }
29 
30     bool maybe_taken(GlobalState gs, StaticGameObject the_hero)
31     {
32         float width = 2.5;
33         if (model == "stone-1" || model == "stone-2") width = 2.0;
34         return !inventory && !used &&
35                 the_hero.x-width <= x && x <= the_hero.x+width &&
36                 the_hero.y <= y+1.0 && y+1.0 <= the_hero.y+2.5 &&
37                 the_hero.z-1.0 <= z && z <= the_hero.z+1.0;
38     }
39 
40     override void draw(GlobalState gs)
41     {
42         if (!inventory && !used &&
43             abs(root.scrx-x) < 16.0 &&
44             abs(root.scry-y) < 9.0)
45         {
46             glPushMatrix();
47             glTranslatef(x, y, z);
48             recursive_render(gs, models[model]);
49             glPopMatrix();
50         }   
51     }    
52 
53     override bool tick(GlobalState gs)
54     {
55         return true;
56     }
57 
58     override void load(string[string] s)
59     {
60         if ("item-"~model~"-x" in s)
61             x = s["item-"~model~"-x"].to!(float);
62         else
63             x = def_x;
64             
65         if ("item-"~model~"-y" in s)
66             y = s["item-"~model~"-y"].to!(float);
67         else
68             y = def_y;
69             
70         if ("item-"~model~"-z" in s)
71             z = s["item-"~model~"-z"].to!(float);
72         else
73             z = def_z;
74 
75         if ("item-"~model in s)
76         {
77             used = (s["item-"~model] == "used");
78             inventory = (s["item-"~model] == "taken");
79         }
80         else
81         {
82             used = false;
83             inventory = false;
84         }
85     }
86 
87     override void save(ref string[string] s)
88     {
89         if (used)
90             s["item-"~model] = "used";
91         else if (inventory)
92             s["item-"~model] = "taken";
93         else
94         {
95             if (x != def_x) s["item-"~model~"-x"] = x.to!(string);
96             if (y != def_y) s["item-"~model~"-y"] = y.to!(string);
97             if (z != def_z) s["item-"~model~"-z"] = z.to!(string);
98         }
99     }    
100 }